<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\common\services\system\attachment;

use app\common\services\BaseServices;
use app\common\dao\system\attachment\SystemAttachmentCategoryDao;
use hdphp\exceptions\AdminException;

/**
 * Class：系统附件分类服务
 * 创建人：Jack
 * 创建时间：2024/1/29 14:46
 * @package app\common\services\system\attachment
 * @mixin SystemAttachmentCategoryDao
 */
class SystemAttachmentCategoryServices extends BaseServices
{
    /**
     * 构造函数
     * @param SystemAttachmentCategoryDao $dao
     */
    public function __construct(SystemAttachmentCategoryDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 注释：获取分类列表
     * 创建人：Jack
     * 创建时间：2024/1/29 14:53
     * @param array $where
     * @return array
     */
    public function getAll(array $where)
    {
        $list = $this->dao->getList($where);
        foreach ($list as &$item) {
            $item['title'] = $item['name'];
            $item['children'] = [];
            if ($where['name'] == '' && $this->dao->count(['pid' => $item['id'], 'file_type' => $where['file_type']])) $item['loading'] = false;
        }
        return compact('list');
    }

    /**
     * 注释：获取分类列表（添加修改）
     * 创建人：Jack
     * 创建时间：2024/1/29 15:09
     * @param array $where
     * @return array[]
     */
    public function getCateList(array $where)
    {
        $list = $this->dao->getList($where);
        $options = [['value' => 0, 'label' => '所有分类']];
        foreach ($list as $id => $cateName) {
            $options[] = ['label' => $cateName['name'], 'value' => $cateName['id']];
        }
        return $options;
    }

    /**
     * 注释：添加附件分类
     * 创建人：Jack
     * 创建时间：2024/1/29 15:07
     * @param array $data
     * @return \hdphp\basic\BaseModel|\think\Model
     */
    public function save(array $data)
    {
        if ($this->dao->getOne(['name' => $data['name'], 'relation_id' => $data['relation_id'] ?? 0])) {
            throw new AdminException('该分类已经存在!');
        }
        $res = $this->dao->save($data);
        if (!$res) throw new AdminException('新增失败!');
        return $res;
    }

    /**
     * 注释：修改附件分类
     * 创建人：Jack
     * 创建时间：2024/1/29 15:08
     * @param int   $id
     * @param array $data
     * @return void
     */
    public function update(int $id, array $data)
    {
        $attachment = $this->dao->getOne(['name' => $data['name'], 'relation_id' => $data['relation_id'] ?? 0]);
        if ($attachment && $attachment['id'] != $id) {
            throw new AdminException('该分类已经存在!');
        }
        $res = $this->dao->update($id, $data);
        if (!$res) throw new AdminException('编辑失败!');
    }

    /**
     * 注释：删除附件分类
     * 创建人：Jack
     * 创建时间：2024/1/29 15:09
     * @param int $id
     * @return void
     */
    public function delete(int $id)
    {
        $count = $this->dao->getCount(['pid' => $id]);
        if ($count) {
            throw new AdminException('请先删除下级分类!');
        } else {
            $res = $this->dao->delete($id);
            if (!$res) throw new AdminException('请先删除下级分类!');
        }
    }

}
